<?php
namespace eagle\modules\util\helpers;

/*+----------------------------------------------------------------------
| 小老板
+----------------------------------------------------------------------
| Copyright (c) 2011 http://www.xiaolaoban.cn All rights reserved.
+----------------------------------------------------------------------
| Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
+----------------------------------------------------------------------
| Author: hxl <plokplokplok@163.com>
+----------------------------------------------------------------------
| Create Date: 2014-01-30
+----------------------------------------------------------------------
 */

/**
 +------------------------------------------------------------------------------
 * result返回参数处理类
 +------------------------------------------------------------------------------
 * @category	Order
 * @package		Controller/order
 * @subpackage  Exception
 * @author		hxl <plokplokplok@163.com>
 * @version		1.0
 +------------------------------------------------------------------------------
 */
class ResultHelper {
	/**
	 +----------------------------------------------------------
	 * 获取返回数据
	 +----------------------------------------------------------
	 * @access static
	 +----------------------------------------------------------
	 * @param code			状态代码
	 * @param data			返回附带数据
	 * @param message		返回信息
	 * @param resultType	返回数据类型 1 json 2 array
	 +----------------------------------------------------------
	 * @return				result
	 +----------------------------------------------------------
	 * log			name	date					note
	 * @author		hxl		2014/02/19				初始化
	 +----------------------------------------------------------
	**/
	public static function getResult($code, $data, $message, $resultType = 1) {
		$result = array(
			'code' => $code,
			'data' => $data,
			'message' => $message
		);
		return $resultType == 1 ? json_encode($result) : $result;
	}

	/**
	 +----------------------------------------------------------
	 * 操作成功
	 +----------------------------------------------------------
	 * @access static
	 +----------------------------------------------------------
	 * @param data			返回附带数据
	 * @param resultType	返回数据类型 1 json 2 array
	 +----------------------------------------------------------
	 * @return				result
	 +----------------------------------------------------------
	 * log			name	date					note
	 * @author		hxl		2014/02/19				初始化
	 +----------------------------------------------------------
	**/
	public static function getSuccess($data = '', $resultType = 1 , $message = '操作成功！') {
		return self::getResult(200, $data, $message , $resultType);
	}

	/**
	 +----------------------------------------------------------
	 * 操作失败
	 +----------------------------------------------------------
	 * @access static
	 +----------------------------------------------------------
	 * @param data			返回附带数据
	 * @param resultType	返回数据类型 1 json 2 array
	 +----------------------------------------------------------
	 * @return				result
	 +----------------------------------------------------------
	 * log			name	date					note
	 * @author		hxl		2014/02/19				初始化
	 +----------------------------------------------------------
	**/
	public static function getFailed($data, $resultType = 1, $message='操作失败！') {
		return self::getResult(400, $data, $message , $resultType);
	}

	/**
	 +----------------------------------------------------------
	 * 参数缺失
	 +----------------------------------------------------------
	 * @access static
	 +----------------------------------------------------------
	 * @param data			返回附带数据
	 * @param resultType	返回数据类型 1 json 2 array
	 +----------------------------------------------------------
	 * @return				result
	 +----------------------------------------------------------
	 * log			name	date					note
	 * @author		hxl		2014/02/19				初始化
	 +----------------------------------------------------------
	**/
	public static function getMissingParameter($data, $resultType = 1, $message='参数缺失！') {
		return self::getResult(401, $data, $message , $resultType);
	}
	
	/**
	 +----------------------------------------------------------
	 * 把小数以百分比格式输出，保留n位小数，例如 2位小数是 86.26%
	 +----------------------------------------------------------
	 * @access static
	 +----------------------------------------------------------
	 * @param data			返回该数字的百分比格式
	 * @param n				返回该数字的百分比格式，n位小数，默认2
	 +----------------------------------------------------------
	 * @return				result
	 +----------------------------------------------------------
	 * log			name	date					note
	 * @author		yzq		2015-3-17				初始化
	 +----------------------------------------------------------
	 **/
	public static function formatPercentage($num,$digit=2) {
		return number_format($num * 100 , $digit)."%";
	}
	
}
